package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.context.BaseContext;
import com.heima.mapper.*;
import com.heima.pojo.dto.NodeAddDTO;
import com.heima.pojo.dto.NodePageDTO;
import com.heima.pojo.dto.NodeUpdateDTO;
import com.heima.pojo.entity.Business;
import com.heima.pojo.entity.Node;
import com.heima.pojo.entity.VmType;
import com.heima.pojo.vo.NodeBsbusinessTypeVO;
import com.heima.pojo.vo.RegionVO;
import com.heima.pojo.vo.VendingMachineDetailsVO;
import com.heima.result.PageResult;
import com.heima.service.NodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    VendingMachineMapper vendingMachineMapper;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    VmTypeMapper vmTypeMapper;

    /**
     * 区域查询
     */
    //TODO
    @Override
    public PageResult<NodeBsbusinessTypeVO> pageSelect(NodePageDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<NodeBsbusinessTypeVO> page = nodeMapper.pageNode(dto);
        page.forEach(n -> {
            RegionVO region = regionMapper.nodeRegion(n.getRegionId());
            List<Node> list = nodeMapper.select(region.getId());
            region.setNodeCount(list.size());
            region.setNodeList(null);
            Business businessType = businessMapper.nodebusinessByid(n.getBusinessId());//待修改
            Integer vmCount = vendingMachineMapper.selectNodeId(n.getId());
            n.setVmCount(vmCount);// 填充region信息
            n.setRegion(region);//填充商圈信息
            n.setBusinessType(businessType); // 填充vm数量
        });
        return new PageResult<>(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    /**
     * 查询商圈列表
     */
    @Override
    public List<Business> selectBusiness() {
        List<Business> businesses = businessMapper.selectBusiness();
        return businesses;
    }

    /**
     * 点位新增
     */
    @Override
    public boolean add(NodeAddDTO dto) {
        Node node = new Node();
        node.setAreaCode(String.valueOf(dto.getAreaCode()));//类型不同单独赋值
        node.setRegionId(Long.parseLong(dto.getRegionId()));
        node.setCreateUserId(1);
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dto, node);
        int result = nodeMapper.add(node);
        return result > 0;
    }

    /**
     * 点位修改
     */
    @Override
    public boolean update(String nodeId, NodeUpdateDTO dto) {
        Node node = new Node();
        node.setId(Long.parseLong(nodeId));
        BeanUtils.copyProperties(dto, node);
        node.setUpdateTime(LocalDateTime.now());
        int result = nodeMapper.update(node);
        return result > 0;
    }

    /**
     * 删除点位
     */
    @Override
    public boolean delete(String nodeId) {
        int result = nodeMapper.delete(Long.parseLong(nodeId));
        return result > 0;
    }
    /**
     * 点位详情 (details)
     */
    @Override
    public List<VendingMachineDetailsVO> NodeDetails(String nodeId) {
        List<VendingMachineDetailsVO> vmd= vendingMachineMapper.nodeSelectList(nodeId);
        vmd.forEach(vm->{
                VmType vmType= vmTypeMapper.selectVM(vm.getVmType());
                vm.setType(vmType);
            NodeBsbusinessTypeVO node = nodeMapper.selectVm(vm.getNodeId());
            RegionVO region = regionMapper.nodeRegion(vm.getRegionId());
            List<Node> list = nodeMapper.select(region.getId());
            region.setNodeCount(list.size());
            region.setNodeList(null);
            Business businessType = businessMapper.nodebusinessByid(vm.getBusinessId());//待修改
            Integer vmCount = vendingMachineMapper.selectNodeId(vm.getId());
            node.setVmCount(vmCount);// 填充region信息
            node.setRegion(region);//填充商圈信息
            node.setBusinessType(businessType); // 填充vm数量
            vm.setRegion(region);
            vm.setNode(node);
            });
        return vmd;
    }
}

